博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Getting started with Java EE 8 MVC(1)
阅读量:7067 次
发布时间:2019-06-28

本文共 5799 字,大约阅读时间需要 19 分钟。

  hot3.png

Getting started with Java EE 8 MVC

MVC is a new specification introduced in the upcoming Java EE 8.

It is based on the existing JAXRS.

At the moment I wrote down these posts, most of Java EE 8 specficitaions are still in the early disscussion stage, and MVC 1.0 is also not finalized, maybe some changes are included in future. I will update the and codes aglined with final Java EE 8 specficitaions when it is released.

I will use the latest Java 8, Glassfish 4.1.1, and NetBeans IDE for these posts.

Prequisition

  • Oracle JDK 8 or OpenJDK 8

    Oracle Java 8 is required, go to to download it and install into your system.

    Optionally, you can set JAVA_HOME environment variable and add <JDK installation dir>/bin in your PATH environment variable.

  • The latest Apache Maven

    Download the latest Apache Maven from , and uncompress it into your local system.

    Optionally, you can set M2_HOME environment varible, and also do not forget to append <Maven Installation dir>/bin your PATH environment variable.

  • NetBeans IDE

    Download the latest NetBeans IDE from , and installed it into your local disk.

  • Glassfish Server

    Download the latest Glassfish from . Currently the latest GA version is 4.1.1. Extracted it into your local disk.

NOTE: You can download the JDK and NetBeans bundle from Oracle website instead of installing JDK and NetBeans IDE respectively.

After you installed all of these, start NetBeans IDE, add Glassfish into Service tab in NetBeans IDE.

The Sample application

To demonstrate the basic usage of MVC spection, I will port the task board sample which I have implemented in the to demonstrate Spring MVC.

Simply, it includes the following features.

  1. List tasks by status, display the tasks in 3 columns(like a simple kanban).
  2. Create a new task.
  3. Edit and update task.
  4. Update task stauts(move to different columns in the task list).
  5. Delete task if it is done.

Create a project skeleton

First of first, you should create a simple project skeleton as start point.

If you are using NetBeans IDE, it is easy to create a Maven based Java EE 7 web project in IDE directly, and add the registered Glassfish as runtime server.

It should include Java EE 7 web artifact as the dependency.

javax
javaee-web-api
7.0
provided

Add additional MVC api and the implemnetation dependencies.

javax.mvc
javax.mvc-api
1.0-edr2
org.glassfish.ozark
ozark
1.0.0-m02
runtime

ozark is the default reference implemnetation of MVC 1.0 specificaiton, which will shipped with Glassfish 5. Currently it is still under active development, what we are using here may be changed in the future.

Here we used Glassfish 4.1.1 as target runtime, so you should include them in the deployment package.

When Glassfish 5 is ready for Java EE 8, these two dependencies can be excluded and removed from POM.

Declare the MVC application

MVC does not reinvent the wheel, it reuses the effort of JAXRS specificaiton.

Similar with activiating JAXRS application, You can declare a customApplicationas our MVC application entry.

@ApplicationPath("mvc")public class MvcConfig extends Application {    @Override    public Set
> getClasses() { return Collections.singleton(TaskController.class); }}

TaskControlleris a controller, it acts as the C in MVC pattern.

Controller

MVC uses a@Controllerannotation to declare a JAXES resource as Controller.

@Path("tasks")@Controllerpublic class TaskController {    @GET    @View("tasks.jspx")    public void allTasks() {        log.log(Level.INFO, "fetching all tasks");        List
todotasks = taskRepository.findByStatus(Task.Status.TODO); List
doingtasks = taskRepository.findByStatus(Task.Status.DOING); List
donetasks = taskRepository.findByStatus(Task.Status.DONE); log.log(Level.INFO, "got all tasks: todotasks@{0}, doingtasks@{1}, donetasks@{2}", new Object[]{todotasks.size(), doingtasks.size(), donetasks.size()}); models.put("todotasks", todotasks); models.put("doingtasks", doingtasks); models.put("donetasks", donetasks); }}

@Viewannotation indicates the view(eg. JSP pages) a void method will return.

Model

Modelsis a contrainer to hold the model datas that will be transfered to the view.

View

Have a look at thetasks.jspxfile, I just copied some code snippets here, please checkout the source codes for details.

TODO

Tasks newly added in the backlog.

  • #${task.id} ${task.name}

    ${task.description}

No surprise, just pure JSP files, I used the JSP xml form in this sample.

By default the views should be put in the /WEB-INF/views folder in projects.

In this example, when you send a GET request to /ee8-mvc/mvc/tasks, theallTasks()method will handle this request, then find data(tasks by status here) from database, and put the query results into aModelscontainer, in the view pages the model data can be accessed via el directly.

The path(/ee8-mvc/mvc/tasks) is the combination of context path, mvc application path, and controller path.

Source codes

  1. Clone the codes from my github.com account.

  2. Open the mvc project in NetBeans IDE.

  3. Run it on Glassfish server.
  4. After it is deployed and running on the Glassfish application server, navigate in your favorite browser.

    https://github.com/hantsy/ee8-sandbox/wiki/mvc-tasks.png

转载于:https://my.oschina.net/hantsy/blog/661428

你可能感兴趣的文章
20135203齐岳信息安全系统设计基础——实验二实验报告
查看>>
剑指Offer 04 二维数组中的查找
查看>>
有关于Git的基础
查看>>
附加作业
查看>>
asp.net成员资格与角色管理使用和配置
查看>>
Python的数据库操作
查看>>
(转)ubuntu简单命令
查看>>
转载:查看linux重启时间方法
查看>>
Eclipse快捷键 10个最有用的快捷键(快捷键都是可以自己定义的,以满足自己的使用习惯)...
查看>>
SQL事务日志备份时的问题
查看>>
HDU-1081-To The Max
查看>>
【海洋女神原创】How to: Installshield做安装包时如何添加文件
查看>>
会话标识未更新
查看>>
XCode使用技巧
查看>>
剑指offer数组2
查看>>
python基础之生成器迭代器
查看>>
python系统编程(二)
查看>>
洛谷P2894 [USACO08FEB]酒店Hotel
查看>>
bzoj千题计划159:bzoj2055: 80人环游世界(有源汇上下界可行最小费用流)
查看>>
pyhton3解决"tuple parameter unpacking is not supported"问题
查看>>